package com.hst.capacity.infra.persistence.jpa.setting.price;

import com.hst.capacity.domain.model.entity.setting.price.AirportCommonPriceEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface AirportCommonPriceRepository extends JpaRepository<AirportCommonPriceEntity, String> {

    AirportCommonPriceEntity findByIdServicePriceType(String idPrice);

    @Query("select t from AirportCommonPriceEntity t left join AirportEntity a on t.idAirport=a.idAirport where t.idFranchisee=:idFranchisee")
    Page<AirportCommonPriceEntity> findByIdFranchisee(String idFranchisee, PageRequest pageRequest);

    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @param channel
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idAirport = :idAirport and t.channel = :channel")
    List<AirportCommonPriceEntity> findByIdAirportAndChannel(@Param("idAirport")String idAirport, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @param channel
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idFranchisee = :idFranchisee " +
            "and t.idAirport = :idAirport " +
            "and t.idServiceType = :idServiceType " +
            "and t.idVehicleType = :idVehicleType " +
            "and t.channel = :channel")
    AirportCommonPriceEntity findAllByIdAirportAndIdServicePriceTypeAndChannel(@Param("idFranchisee")String idFranchisee, @Param("idAirport")String idAirport, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @param channel
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idAirport = :idAirport " +
            "and t.idServiceType = :idServiceType " +
            "and t.channel = :channel " +
            "and t.status = 1")
    List<AirportCommonPriceEntity> findByIdAirportAndIdServicePriceTypeAndChannel(@Param("idAirport")String idAirport, @Param("idServiceType")int idServiceType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idFranchisee = :idFranchisee " +
            "and t.idAirport = :idAirport " +
            "and t.idServiceType = :idServiceType " +
            "and t.channel = :channel  " +
            "and t.status = 1 " )
    List<AirportCommonPriceEntity> findByIdAirportAndStatusOnline(@Param("idFranchisee")String idFranchisee,
                                                                  @Param("idAirport")String idAirport,
                                                                  @Param("idServiceType")int idServiceType,
                                                                  @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idFranchisee = :idFranchisee " +
            "and t.idAirport = :idAirport " +
            "and t.idServiceType = :idServiceType " +
            "and t.idVehicleType = :idVehicleType " +
            "and t.channel = :channel  " +
            "and t.status = 1 " )
    AirportCommonPriceEntity findByIdAirportAndStatusOnline(@Param("idFranchisee")String idFranchisee, @Param("idAirport")String idAirport, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);


    /**
     * 通过地区查找站点价格
     * @param idAirport
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idFranchisee = :idFranchisee " +
            "and t.idAirport = :idAirport " +
            "and t.idServiceType = :idServiceType " +
            "and t.idVehicleType = :idVehicleType " +
            "and t.channel = :channel")
    AirportCommonPriceEntity findByIdAirport(@Param("idFranchisee")String idFranchisee, @Param("idAirport")String idAirport, @Param("idServiceType")int idServiceType, @Param("idVehicleType")int idVehicleType, @Param("channel")String channel);

    /**
     * 通过地区查找站点价格
     * @param idPrice
     * @return
     */
    @Query("select t " +
            "from AirportCommonPriceEntity t " +
            "where t.idServicePriceType = :idPrice")
    AirportCommonPriceEntity findMarkupByIdServicePriceType(@Param("idPrice") String idPrice);

}